package com.xuecheng.test.fastdfs;

import org.csource.fastdfs.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author zhaoxiang
 * @version 1.0
 * @Date 2019/11/2 11:21
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class testFastDFS {

    /**
     * 上传文件
     */
    @Test
    public void testUpload(){
        try {
            //加载fastDFS-client.properties配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //定义TrackerClient用于请求TrackerServer
            TrackerClient trackerClient = new TrackerClient();
            //连接Tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取Storage
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //创建StorageClient
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            //本地文件名称
            String filePath = "D:/timg (1).jpg";
            //向Storage服务器上传文件  group1/M00/00/00/wKgZhV28_EuEVQcEAAAAALwDcnk581.jpg
            String fileId = storageClient1.upload_appender_file1(filePath, "jpg", null);
            System.out.println(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDownload(){
        try {
            //加载fastDFS-client.properties配置文件
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            //定义TrackerClient用于请求TrackerServer
            TrackerClient trackerClient = new TrackerClient();
            //连接Tracker
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取Storage
            StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
            //创建StorageClient
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
            byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKgZhV28_EuEVQcEAAAAALwDcnk581.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream(new File("e:/gongchengliangtian.jpg"));
            fileOutputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
